package com.yzy.fly.bean;

import com.yzy.fly.function.logging.Log;
import com.yzy.fly.function.logging.LogFactory;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import com.yzy.fly.util.ReflectionsUtil;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Set;

/**
 * ImportBeanDefinitionRegistrar比ImportSelector功能强大
 */
public class FlyBeanRegistrar implements ImportBeanDefinitionRegistrar {
    private static Log log = LogFactory.getLog(FlyBeanRegistrar.class);

    /**
     * 手动注册bean到IOC容器
     * @param importingClassMetadata
     * @param registry
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//        log.info("FlyBeanRegistrar");
//        AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableFly.class.getName()));
//        String name = "";
//        if (annotationAttributes != null) {
//            name = annotationAttributes.getString("value");
//        }
//        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Teacher.class.getName());
//        builder.addPropertyValue("name", name);
//        registry.registerBeanDefinition("teacher", builder.getBeanDefinition());

        Set<Class<? extends FlyBasicMapper>> mapperClasses = null;
        try {
            mapperClasses = ReflectionsUtil.getSubTypesOf(FlyBasicMapper.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("mapperClasses.size:{}",mapperClasses != null ? mapperClasses.size(): 0);
    }
}

